package com.djx.controller;

import com.djx.pojo.GuarInfo;
import com.djx.pojo.to.XXXDto;
import com.djx.utils.ExcelUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName: ExcelController
 * @Description: TODO
 * @author: 星~
 * @date: 2021/8/14 0014 下午 13:50
 */

@Slf4j
@RestController
public class ExcelController {

    @RequestMapping("/readExcel")
    public List writeWithoutHead(MultipartFile file) throws IOException {
        String originalFilename = file.getOriginalFilename();
        boolean matches = originalFilename.matches("^.+\\.(?i)((xls)|(xlsx))$");

        if (matches) {

            List<XXXDto> baseRowModels = (ExcelUtil.readExcel(new BufferedInputStream(file.getInputStream()), XXXDto.class));
            log.info("Excel读取的数据为" + baseRowModels.size() + "条");

            List<GuarInfo> collect = baseRowModels.stream().map(i -> {
                GuarInfo guarInfo = new GuarInfo();
                BeanUtils.copyProperties(i, guarInfo);
                return guarInfo;
            }).collect(Collectors.toList());

            collect.stream().forEach(System.out::println);

            List<String> ids = baseRowModels.stream()
                    .filter(item -> item.getWhetherTheAuthorizationIsSuccessful() != null)
                    .filter(item -> StringUtils.isNotBlank(item.getWhetherTheAuthorizationIsSuccessful()))
                    .filter(item -> item.getWhetherTheAuthorizationIsSuccessful().trim().equals("是"))
                    .collect(Collectors.toSet()).stream().map(item -> {
                        return item.getOrderId();
                    }).collect(Collectors.toList());
            return ids;

        } else {
            log.info("文件不是excel格式");
            return null;
        }
    }



}
